﻿"use strict";
define(['admin/app', "service/admin/itpService","service/admin/transferService"], function (app) {
    app.controller('itpController', function ($scope, $stateParams, $location, $uibModal, $state, $filter, utils, w5cValidator, itpService,transferService) {
        $scope.query = {
            page: 1, size: 20, start_date: '', end_date: '', status: 0, pay_channel: 0
        };
        $scope.itp_form={create_date:'',add:false};
        $.extend($scope.query, $stateParams);
        if($scope.query.pay_channel==1)$scope.breadcrumb='HF对账';
        else if($scope.query.pay_channel==3)$scope.breadcrumb='Hx对账';
        var methods = {
            search: function () {
                itpService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            formatDate:function(dt,format){
	            var time = new Date(dt);
				var result = $filter('date')(time, format);
				return result;
            },
            search_page:function(page){
            	$scope.query.page = page;
	            $location.search('page', $scope.query.page);
            },
            get: function () {
                if ($stateParams.id > 0) {
                    itpService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '详情';
                }
            },
            goBack: function () {
                history.back();
            },
            add:function(){
    			$scope.itp_form={create_date:'',add:!$scope.itp_form.add};
	            w5cValidator.setRules({
	                create_date: { required: '请选择日期' }
	            });
            },
            code_file: function () {
                $scope.psdisabled = true;
            	itpService.code_file($scope.itp_form).success(function (response) {
                   utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        methods.search();
                    }
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            deal: function (item) {
                $scope.psdisabled = true;
            	itpService.deal({pay_channel:item.pay_channel,id:item.id}).success(function (response) {
                   utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        methods.search();
                    }
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            get_balance: function () {
                $scope.psdisabled = true;
            	transferService.get_balance({pay_channel:$scope.query.pay_channel}).success(function (response) {
                   utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        $scope.model.channel_total=response.total;
                        $scope.model.balance=response.balance;
                    }
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.finance.itp')) {
        	methods.search();
    	}
    });
});
